<?php
namespace app\wxapp\controller;

use app\model\Theme; 
use app\model\Poster; 
use app\model\Yxgifts as Yxgifts_mod;

class Yxgifts extends Base
{
    public function Yxgifts()
    {
/**
 * Author: cbw
 * Desc: 摇一摇多礼品
 * Time: 2022/3/30 08:28
 * Email: 17854288794@163.com
 */
global $_G,$_P;
$fans = $this->checkuser();
$system = $_G['system'];
$themeid = intval($_P['themeid']);
$op = $_P['op']?$_P['op']:'get';
if($themeid <= 0){
    return_json(1,'活动入口不正确');
}
$config =   Theme::getConfig($themeid);
$theme = Theme::getThemeById($themeid);
//获取礼品
if ($op=='get'){
    $res = Yxgifts_mod::getGift($config,$themeid,$fans);
    return $res;
}
//分享
if ($op=='pyq'){
    $gid = $_P['gid'];
    $where = ['gid'=>$gid,'uid'=>$fans['uid'],'themeid'=>$themeid];
    $edit = ['status'=>2];
    $res = Yxgifts_mod::editFanGift($where,$edit);
    if (!$res){
        return_json(1,'操作失败');
    }else{
        return_json(0,'分享成功快去查看礼品');
    }
}
//客资收集
if ($op=='gift_user_info'){
    if (!isset($_P['username']) || !isset($_P['mobile']) || !$_P['username'] || !$_P['mobile']){
        return_json(1,'请输入完整信息');
    }
    $res = Yxgifts_mod::updateFanInfo($_P,$fans,$themeid);
    return $res;
}
//我的摇一摇礼品
if ($op=='my_gifts'){
    //待转发朋友圈 为失效
    $edit_where1 = ['themeid'=>$themeid,'uid'=>$fans['uid'],'status'=>1];
    $edit_data1 = ['status'=>11];
    $result1 = Yxgifts_mod::editFanGift($edit_where1,$edit_data1);
    //判断用户客资是否填写
    if ($fans['username'] && $fans['mobile']){//更新所有待客资为待兑换
        $edit_where = ['themeid'=>$themeid,'uid'=>$fans['uid'],'status'=>2];
        $edit_data = ['status'=>3];
        $result = Yxgifts_mod::editFanGift($edit_where,$edit_data);

    }
    $where = ['uid'=>$fans['uid'],'themeid'=>$themeid];
    $res = Yxgifts_mod::getUserAllGifts($where);
    $config_gifts = Yxgifts_mod::getFormatGifts($config);

    //格式化
    foreach ($res as $key => $val){
        $res[$key]['gname'] = $config_gifts[$val['gid']]['yx_gift_name'];
        $res[$key]['gimg'] = $config_gifts[$val['gid']]['yx_gift_img'];
        $res[$key]['convert_url'] = '';
        switch ($val['status']){
            case 1:
                $res[$key]['btn_txt'] = '待发圈';
                break;
            case 11:
                $res[$key]['btn_txt'] = '已失效';
                break;
            case 2:
                $res[$key]['btn_txt'] = '填写客资';
                break;
            case 3:
                $scene = MODULE_NAME . "-" . $themeid . "-".$fans['uid']."-" . $theme['softkey']."-".$val['id'];
                $inputurl = Poster::CreateScene($system, $scene, $config, 'Worker/pages/w_gifts/w_gifts');
                $res[$key]['btn_txt'] = '待兑换';
//                $res[$key]['convert_url'] = $inputurl;
                $res[$key]['convert_url'] = "http://img2020.yixuansoft.cn/images/poster/2021-12/yx18900/6f9510a784e583617a0fc84912ef3f73.png";
                break;
            case 4:
                $res[$key]['btn_txt'] = '已兑换';
                break;
            default:
                $res[$key]['btn_txt'] ='';
                break;
        }
    }
    $rules = $config['yx_gt_rules'];
    return_json(0,'ok',['list'=>$res,'rules'=>$rules]);
}

}
}